<?php
/**
 * Created by PhpStorm.
 * User: xing
 * Date: 2018/11/22
 * Time: 6:42 PM
 */
namespace app\wechat\controller;
use app\enumerations\CustomerVipEnum;

class AccountDetail extends Common {

    use WechatTrait;

    /**
     * 收入明细
     * @return \think\response\Json
     */
    public function incomeAccountDetailList() {

        $currentCustomer = $this->request->currentCustomer;
        $pageNum = $this->request->post("pageNum");
        $pageSize = $this->request->post("pageSize");

        $accountLog = $this->accountDetailService->getAccountListForPage(
            $currentCustomer['id'], $pageNum, $pageSize);

        foreach ($accountLog as &$val) {

            $val['date_time'] = formatAccountDetailTime($val['create_time']);

            // 历史问题，如果leftJoin出来的数据为空则默认显示自己
            $val['nickname'] = isNullOrEmpty($val['nickname'])
                ? $currentCustomer['nickname'] : $val['nickname'];
            $val['headimgurl'] = isNullOrEmpty($val['headimgurl'])
                ? $currentCustomer['headimgurl'] : $val['headimgurl'];
        }

        // 去除重复值后放到数组中
        $timeData = [];
        foreach ($accountLog as $key=>$val) {
            if (!deep_in_array($val['date_time'], $timeData)){
                $timeData[]['name'] = $val['date_time'];
            }
        }

        // 循环明细列表
        foreach ($accountLog as $key=>$val) {

            // 循环日期数据
            foreach ($timeData as &$v) {

                // 如果相等
                if ($val['date_time'] == $v['name']) {

                    $val['date_time'] = date("m/d H:i",
                        strtotime($val['create_time']));
                    unset($val['create_time']);

                    // 日期数据的item_list 吧明细装进去
                    $v['item_list'][] = $val;
                }
            }
        }

        // 我在每日小店已赚
        $historyProfit = $this->orderService
            ->getHistoryProfitByCustomerId($currentCustomer["id"]);

        // 判断是否为自购vip
        if($currentCustomer["vip"]
            == CustomerVipEnum::getEnumValue(CustomerVipEnum::YES_CODE)
            && $currentCustomer['parent_recommend_customer_id'] == 0) {
            $historyProfit -= config('constants.recommendGiftMoney');
        }

        // 查询当前用户昨天统计数据
        $yesterdayStatistics = $this->customerEverydayStatisticsService
            ->findByCustomerIdAndDay($currentCustomer['id'], date("Y-m-d", strtotime("-1 day")));

        $returnData['info'] = $this->baseCustomerInfo();
        $returnData['info']['history_profit'] = sprintf('%.2f', $historyProfit);
        $returnData['info']['yesterday_profit_amt'] = isNullOrEmpty($yesterdayStatistics['total_profit_amt'])
            ? 0 : $yesterdayStatistics['total_profit_amt'];
        $returnData['list'] = $timeData;

        return successReturn($returnData);

    }

    /**
     * 金币流水列表
     * @return \think\response\Json
     */
    public function pointAccountDetailList() {

        $currentCustomer = $this->request->currentCustomer;
        $pageNum = $this->request->post("pageNum");
        $pageSize = $this->request->post("pageSize");

        $accountLog = $this->pointDetailService->getAccountListForPage(
            $currentCustomer['id'], $pageNum, $pageSize);

        foreach ($accountLog as &$val) {

            $val['date_time'] = formatAccountDetailTime($val['create_time']);

            // 历史问题，如果leftJoin出来的数据为空则默认显示自己
            $val['nickname'] = isNullOrEmpty($val['nickname'])
                ? $currentCustomer['nickname'] : $val['nickname'];
            $val['headimgurl'] = isNullOrEmpty($val['headimgurl'])
                ? $currentCustomer['headimgurl'] : $val['headimgurl'];
        }

        // 去除重复值后放到数组中
        $timeData = [];
        foreach ($accountLog as $key=>$val) {
            if (!deep_in_array($val['date_time'], $timeData)){
                $timeData[]['name'] = $val['date_time'];
            }
        }

        // 循环明细列表
        foreach ($accountLog as $key=>$val) {

            // 循环日期数据
            foreach ($timeData as &$v) {

                // 如果相等
                if ($val['date_time'] == $v['name']) {

                    $val['date_time'] = date("m/d H:i",
                        strtotime($val['create_time']));
                    unset($val['create_time']);

                    // 日期数据的item_list 吧明细装进去
                    $v['item_list'][] = $val;
                }
            }
        }

        $returnData['info'] = $this->baseCustomerInfo();
        $returnData['list'] = $timeData;

        return successReturn($returnData);

    }

}